//
//  IdentityAlertView.m
//  Peso Guide
//
//  Created by macbook on 2025/6/13.
//

#import "IdentityAlertView.h"
#import "IdentityAlertViewCell.h"

@interface IdentityAlertView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *grayView;
@property (nonatomic, strong) UIImageView *backImg;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) BaseTableView *tableView;

@property (nonatomic, strong) NSDictionary *chooseChain;

@end

@implementation IdentityAlertView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _grayView = [PesoKit createViewWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) backGroundColor:[UIColor colorWithHexString:@"#000000" alpha:0.7]];
        [self addSubview:_grayView];
        
        _backImg = [PesoKit createImageViewWithFrame:CGRectMake((KScreenWidth-301)/2.0, (KScreenHeight-411)/2.0-20, 301, 411) imageNamed:@"ic_identity_alert_back"];
        _backImg.userInteractionEnabled = YES;
        _backImg.clipsToBounds = YES;
        _backImg.layer.cornerRadius = 15.0f;
        [self addSubview:_backImg];
        
        _titleLabel = [PesoKit createLabelWithFrame:CGRectMake(0, 0, _backImg.width, 60) backGroundColor:[UIColor clearColor] text:[Lang set:@"Please select"] textColor:ColorHex(@"#000000") font:FONT(FontWeightStyleMedium, 20)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_titleLabel];
        
        _confirmButton = [PesoKit createButtonWithFrame:CGRectMake((_backImg.width-214)/2.0, _backImg.height-32-44, 214, 44) backGroundColor:ColorHex(@"#FE7133") title:[Lang set:@"Confirm"] titleColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 18)];
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.clipsToBounds = YES;
        _confirmButton.layer.cornerRadius = 22.0f;
        [_backImg addSubview:_confirmButton];
        
        _closeButton = [[UIButton alloc] initWithFrame:CGRectMake((KScreenWidth-26)/2.0, _backImg.bottom+16, 26, 26)];
        [_closeButton setBackgroundImage:[UIImage imageNamed:@"ic_identity_close"] forState:0];
        [_closeButton addTarget:self action:@selector(closeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeButton];
        
        [_backImg addSubview:self.tableView];
        
        if (self.dataArray.count > 0) {
            
            for (int i = 0; i < self.dataArray.count; i++) {
                NSMutableDictionary *dataDic = [NSMutableDictionary safeDictionaryWithDictionary:[self.dataArray safeObjectAtIndex:i]];
                if (i == 0) {
                    [dataDic setObject:@"1" forKey:@"isChoose"];
                    self.chooseChain = [NSDictionary safeDictionaryWithDictionary:dataDic];
                }else {
                    [dataDic setObject:@"0" forKey:@"isChoose"];
                }
                [self.dataArray replaceObjectAtIndex:i withObject:dataDic];
            }
            
            [self.tableView reloadData];
        }
    }
    return self;
}

- (void)confirmButtonClick {
    
    if (self.contactInfoVC) {
        if ([AppTools isEmpty:self.contactInfoIndex] || [AppTools isEmpty:self.chooseChain]) {
            [SVProgressHUD showErrorWithStatus:@"please select"];
            [SVProgressHUD dismissWithDelay:3.0f];

            return;
        }
        self.contactInfoVC.chooseTypeIndex = self.contactInfoIndex;
        self.contactInfoVC.chooseType = self.chooseChain;
    }
    
    if (self.selectionHandler) {
        if ([AppTools isEmpty:self.chooseChain]) {
            [SVProgressHUD showErrorWithStatus:@"please select"];
            [SVProgressHUD dismissWithDelay:3.0f];

            return;
        }
        self.selectionHandler(self.chooseChain);
    }
    [self removeFromSuperview];
}

- (void)closeButtonClick {
    [self removeFromSuperview];
}

#pragma mark - UITableViewDelegate&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *IdentityAlertViewCellIdentifier = [NSString stringWithFormat:@"IdentityAlertViewCellIdentifier%ld",(long)indexPath.row];
    IdentityAlertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IdentityAlertViewCellIdentifier];
    if (!cell) {
        cell = [[IdentityAlertViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IdentityAlertViewCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataArray.count > 0 ) {
        cell.dataDic = [self.dataArray safeObjectAtIndex:indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (int i = 0; i < self.dataArray.count; i++) {
        NSMutableDictionary *dataDic = [NSMutableDictionary safeDictionaryWithDictionary:[self.dataArray safeObjectAtIndex:i]];
        
        if (indexPath.row == i) {
            [dataDic setObject:@"1" forKey:@"isChoose"];
            self.chooseChain = [NSDictionary safeDictionaryWithDictionary:dataDic];
        }else {
            [dataDic setObject:@"0" forKey:@"isChoose"];
        }
        
        [self.dataArray replaceObjectAtIndex:i withObject:dataDic];
    }
    [self.tableView reloadData];
    DLog(@"=-=--=-=-=-=-=-=%@",self.chooseChain);
}

#pragma mark - lazy load
- (BaseTableView *)tableView {
    if (!_tableView) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(16, 60, 301-16*2, 335-40-16-16) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
